﻿namespace BLL
{
    using DAL;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class TeamController
    {
        private ConnectionManager connMgr = new ConnectionManager();

        public int GetTeamGestor(int idTeam)
        {
            SqlCommand qryX = new SqlCommand("SELECT idGestor FROM Times WHERE idTime = @idTime");
            qryX.Parameters.AddWithValue("@idTime", idTeam);
            DataRow dr = this.connMgr.doReader(qryX).Rows[0];
            return Convert.ToInt32(dr[0]);
        }

        public string GetTeamName(int idTeam)
        {
            SqlCommand qryX = new SqlCommand("SELECT DescTime FROM Times WHERE idTime = @idTime");
            qryX.Parameters.AddWithValue("@idTime", idTeam);
            DataRow dr = this.connMgr.doReader(qryX).Rows[0];
            return dr[0].ToString();
        }

        public DataTable GetTeams()
        {
            SqlCommand qryX = new SqlCommand("SELECT T.idTime, T.DescTime, T.idGestor, U.Nome as Gestor FROM Times as T INNER JOIN Usuarios as U ON U.idUsuario = T.idGestor");
            return this.connMgr.doReader(qryX);
        }

        public void NewTeam(int idGestor, string Desc)
        {
            SqlCommand qryX = new SqlCommand("INSERT INTO Times (DescTime,idGestor) VALUES (@Desc,@idGestor)");
            qryX.Parameters.AddWithValue("@Desc", Desc);
            qryX.Parameters.AddWithValue("@idGestor", idGestor);
            this.connMgr.doCommand(qryX);
        }

        public void UpdateTeam(int idTeam, string DescTime, int idGestor)
        {
            SqlCommand qryX = new SqlCommand("UPDATE [Times] SET [DescTime] = @DescTime , [idGestor] = @idGestor WHERE idTime = @idTime");
            qryX.Parameters.AddWithValue("@idTime", idTeam);
            qryX.Parameters.AddWithValue("@DescTime", DescTime);
            qryX.Parameters.AddWithValue("@idGestor", idGestor);
            this.connMgr.doCommand(qryX);
        }
    }
}

